package com.hbisedm.interninfosys.intern.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbisedm.interninfosys.commonutils.R;
import com.hbisedm.interninfosys.intern.entity.ChatFriends;
import com.hbisedm.interninfosys.intern.entity.StuInternship;
import com.hbisedm.interninfosys.intern.entity.StuTeach;
import com.hbisedm.interninfosys.intern.entity.User;
import com.hbisedm.interninfosys.intern.service.IChatFriendsService;
import com.hbisedm.interninfosys.intern.service.IStuInternshipService;
import com.hbisedm.interninfosys.intern.service.IStuTeachService;
import com.hbisedm.interninfosys.intern.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Sam
 * @since 2020-12-03
 */
@RestController
@RequestMapping("/intern/chat-friends")
public class ChatFriendsController {
    @Autowired
    IChatFriendsService chatFriendsService;
    @Autowired
    IUserService userService;
    @Autowired
    IStuTeachService stuTeachService;
    @Autowired
    IStuInternshipService stuInternshipService;

    @PostMapping("/add")
    public R add(@RequestBody ChatFriends chatFriends) {
        boolean save = chatFriendsService.save(chatFriends);
        return R.ok().data("status", save);
    }

    /**
     * 获取好友表
     * @param id
     * @return
     */
    @GetMapping("/getFriends/{id}")
    public R getFriends(@PathVariable("id") Integer id){
        QueryWrapper<ChatFriends> chatFriendsQueryWrapper = new QueryWrapper<>();
        chatFriendsQueryWrapper.eq("USER_ID", id);
        List<ChatFriends> list = chatFriendsService.list(chatFriendsQueryWrapper);
        for (ChatFriends chatFriends : list) {
            chatFriends.setFriendName(userService.getById(chatFriends.getFriendId()).getOthername());
        }
        return R.ok().data("friends", list);
    }

    /**
     * 通过id 获取他的教师和企业
     * 通过id 获取他的学生
     * @param id
     * @return
     */
    @GetMapping("/otherGetFriends/{id}")
    public R otherGetFriends(@PathVariable("id") Integer id){
        List<ChatFriends> list = new ArrayList<>();
        User user = userService.getById(id);
        /**
         * 学生
         */
        if (user.getType() == 1) {
            StuTeach stuTeach = stuTeachService.getBaseMapper().selectOne(new QueryWrapper<StuTeach>().lambda().eq(StuTeach::getSid, id));
            StuInternship internship = stuInternshipService.getBaseMapper().selectOne(new QueryWrapper<StuInternship>().lambda().eq(StuInternship::getSid, id));
            ChatFriends chatFriends01 = new ChatFriends();
            if(stuTeach!= null) {
                User teach = userService.getById(stuTeach.getTid());
                chatFriends01.setUserId(id);
                chatFriends01.setFriendId(stuTeach.getTid());
                chatFriends01.setFriendName(teach.getOthername());
            }
            ChatFriends chatFriends02 = new ChatFriends();
            if(internship != null) {
                User intern = userService.getById(internship.getIid());
                chatFriends02.setUserId(id);
                chatFriends02.setFriendId(intern.getId());
                chatFriends02.setFriendName(intern.getOthername());
            }
            if(chatFriends01.getFriendId() != null) {
                list.add(chatFriends01);
            }
            if(chatFriends02.getFriendId() != null) {
                list.add(chatFriends02);
            }
        } else {
            /**
             * 教师
             */
            if(user.getType() == 2) {

                List<StuTeach> studentList = stuTeachService.list(new QueryWrapper<StuTeach>().lambda().eq(StuTeach::getTid, id));
                for (StuTeach stuTeach : studentList) {
                    ChatFriends chatFriends = new ChatFriends();
                    chatFriends.setUserId(id);
                    chatFriends.setFriendId(stuTeach.getSid());
                    chatFriends.setFriendName(userService.getById(stuTeach.getSid()).getOthername());
                    list.add(chatFriends);
                }
            }
            /**
             * 企业
             */
            if(user.getType() == 3) {
                List<StuInternship> internshipList = stuInternshipService.list(new QueryWrapper<StuInternship>().lambda().eq(StuInternship::getIid, id));
                for (StuInternship internship : internshipList) {
                    ChatFriends chatFriends = new ChatFriends();
                    chatFriends.setUserId(id);
                    chatFriends.setFriendId(internship.getSid());
                    chatFriends.setFriendName(userService.getById(internship.getSid()).getOthername());
                    list.add(chatFriends);
                }
            }
        }

//        QueryWrapper<ChatFriends> chatFriendsQueryWrapper = new QueryWrapper<>();
//        chatFriendsQueryWrapper.eq("USER_ID", id);
//        list = chatFriendsService.list(chatFriendsQueryWrapper);
//        for (ChatFriends chatFriends : list) {
//            chatFriends.setFriendName(userService.getById(chatFriends.getFriendId()).getOthername());
//        }
        return R.ok().data("friends", list);
    }


}
